package at.codebase.web.servlets.user;


/**
 * A Input Status is used to verify a Argument/Input. It proves the raw data and
 * converts it to the needed type.
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 * 
 */
public class UserInputStatus {
	
	public static class InputStatusUsername extends InputStatus<String, String> {

		public InputStatusUsername(String rawValue) {
			super(rawValue);
		}

		@Override
		public void compile() {
			if (rawValue != null) {
				String raw = rawValue.trim();
				if (raw.matches("[a-zA-Z0-9äöüÄÖÜ]{5,20}")) {
					error = false;
					value = raw;
					return;
				}
			}
			error = true;
		}

	}

	public static class InputStatusDescription extends
			InputStatus<String, String> {

		public InputStatusDescription(String rawValue) {
			super(rawValue);
		}

		@Override
		public void compile() {
			if (rawValue != null) {
				String raw = rawValue.trim();
				if (raw.matches("(.|\\n|\\r)*")) {
					error = false;
					value = raw;
					return;
				}
			}
			error = true;
		}

	}

	public static class InputStatusVoid extends InputStatus<String, String> {

		public InputStatusVoid(String defaultValue) {
			super(defaultValue);
			this.value = "";
		}

		public InputStatusVoid() {
			super("");
			this.value = "";
		}

		@Override
		public void compile() {

		}

	}

	public static abstract class InputStatus<I, E> {
		I rawValue;
		E value;
		boolean error;

		public InputStatus(I rawValue) {
			this.rawValue = rawValue;
			this.value = null;
			this.error = false;

			if (this.rawValue == null) {
				this.error = true;
			}
		}

		public String getRawString() {
			if (rawValue == null)
				return "";
			return rawValue.toString();
		}

		public boolean isError() {
			return error;
		}

		public abstract void compile();

		public E getValue() {
			return value;
		}
	}
}
